package com.andy.dogcoin

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.andy.adapter.DetailAdapter
import com.andy.db.CoinInfo
import com.andy.db.CoinInfoDao
import com.andy.db.DBManager

class CoinDetailActivity : AppCompatActivity() {
    var coinAddr = ""
    companion object{

        /**
         * id没有值的时候，必须传-1
         */
        fun startActivity(context: Context, addr: String) {
            val intent = Intent(context, CoinDetailActivity::class.java)
            intent.putExtra("addr", addr)
            (context as Activity).startActivity(intent)
        }
    }

    private var recyclerView: RecyclerView? = null
    private var mAdapter: DetailAdapter? = null
    var listData: List<CoinInfo>? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_coin_detail_acivity)
        coinAddr = getIntent().getStringExtra("addr")!!

    }

    override fun onResume() {
        super.onResume()
        initData()
        initView()
    }

    fun DetailAddBtn(v: View){
        AndyAddActivity.Jump(this, coinAddr, listData!!.get(0).coin_name)
    }

    fun initData(){
        var dao = CoinInfoDao(this)
        var mDBManager = DBManager.getInstance(this)
        mDBManager.beginTransaction()
        var result = false
        try {
            listData = dao.queryByAddr(coinAddr)
            result = true
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            mDBManager.endTransaction(result)
        }
    }
    fun initView(){
        recyclerView = findViewById(R.id.detail_recycler_view)
        recyclerView?.layoutManager = LinearLayoutManager(this)
        mAdapter = DetailAdapter(listData, this)
        recyclerView?.adapter = mAdapter
    }


}